<template>
  <div class="page_main">
    <div class="search_box">
      <el-form ref="searchForm" :inline="true" :model="searchForm" size="mini">
        <search-form-box>
          <template slot="showSearch">
            <el-form-item label="提交时间" prop="begin_time">
              <el-date-picker
                v-model="searchForm.begin_time"
                type="date"
                placeholder="选择日期"
                value-format="yyyy-MM-dd"
                format="yyyy年MM月dd日"
                style="width: 180px"
              >
              </el-date-picker>
            </el-form-item>
            <el-form-item label="至" prop="end_time">
              <el-date-picker
                v-model="searchForm.end_time"
                type="date"
                placeholder="选择日期"
                value-format="yyyy-MM-dd"
                format="yyyy年MM月dd日"
                style="width: 180px"
              >
              </el-date-picker>
            </el-form-item>
						<el-form-item label="保存时间" prop="begin_time_save">
						  <el-date-picker
						    v-model="searchForm.begin_time_save"
						    type="date"
						    placeholder="选择日期"
						    value-format="yyyy-MM-dd"
						    format="yyyy年MM月dd日"
						    style="width: 180px"
						  >
						  </el-date-picker>
						</el-form-item>
						<el-form-item label="至" prop="end_time_save">
						  <el-date-picker
						    v-model="searchForm.end_time_save"
						    type="date"
						    placeholder="选择日期"
						    value-format="yyyy-MM-dd"
						    format="yyyy年MM月dd日"
						    style="width: 180px"
						  >
						  </el-date-picker>
						</el-form-item>
            <el-form-item label="工单" prop="work_order_id">
              <el-select v-model="searchForm.work_order_id" placeholder="请选择工单" clearable filterable
              >
                <el-option v-for="item in workorderArr" :key="item.id" :label="item.name" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="安装工" prop="worker_id">
              <el-select v-model="searchForm.worker_id" placeholder="请选择安装工" clearable filterable
              >
                <el-option v-for="item in workerArr" :key="item.id" :label="item.name" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="安装类型" prop="work_order_type_id">
              <el-select v-model="searchForm.work_order_type_ids" multiple placeholder="请选择安装类型"
              >
                <el-option label="新安装" :value="1"></el-option>
                <el-option label="上刊" :value="2"></el-option>
                <el-option label="下刊" :value="3"></el-option>
                <el-option label="巡检" :value="4"></el-option>
                <el-option label="维修" :value="5"></el-option>
                <el-option label="换画" :value="7"></el-option>
                <el-option label="中间监播" :value="8"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item prop="status">
              <el-tooltip slot="label" placement="top" content="勾选上时只搜索统一工单下同一点位最新的通过审核的数据">
                <span>工作量统计<i class="el-icon-question"></i></span>
              </el-tooltip>
              <el-checkbox v-model="searchForm.statistics" @change="statisticsChange"></el-checkbox>
            </el-form-item>
          </template>
          <template slot="hideSearch">
            <el-row>
              <el-form-item label="楼盘" prop="building_area_id">
                <el-select v-model="searchForm.building_area_id" placeholder="请选择楼盘" clearable filterable
                >
                  <el-option v-for="item in buildingArr" :key="item.id" :label="item.name" :value="item.id"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="编号" prop="code">
                <el-input v-model="searchForm.code" placeholder="请输入编号" clearable style="width: 200px"></el-input>
              </el-form-item>
              <el-form-item label="媒体类型" prop="media_type_id">
                <el-select v-model="searchForm.media_type_id" placeholder="请选择媒体类型" clearable filterable
                >
                  <el-option v-for="item in allMediaTypeArr" :key="item.id" :label="item.name" :value="item.id"
                  ></el-option>
                </el-select>
              </el-form-item>
              <el-form-item v-show="!searchForm.statistics" label="状态" prop="status">
                <el-select v-model="searchForm.status" placeholder="请选择状态" clearable
                >
                  <el-option label="未通过" value="2"></el-option>
                  <el-option label="待审核" value="3"></el-option>
                  <el-option label="通过" value="4"></el-option>
                </el-select>
              </el-form-item>
            </el-row>
          </template>
          <template slot="searchBtn">
            <el-form-item>
              <el-button type="primary" plain @click="search">搜索</el-button>
              <el-button type="info" plain @click="resetForm('searchForm')">重置</el-button>
            </el-form-item>
          </template>
        </search-form-box>
      </el-form>
    </div>
    <div class="flex flex-no-wrap justify-between">
      <el-button size="mini" @click="exportExcel">导出</el-button>
      <el-popover placement="bottom-start" width="300" trigger="hover">
        <div>
          <div>1、列表中<span class="text-red">红色</span>字体，表示<span class="text-blue">原设备</span>已被注销。</div>
        </div>
        <el-button slot="reference" size="mini" icon="el-icon-warning-outline">说明</el-button>
      </el-popover>
    </div>
    <div class="data_box">
      <el-table id="dataTable" v-loading="tableLoading" :data="tableData" size="mini" stripe border
                :row-class-name="tableRowClassName"
      >
        <el-table-column label="序号" fixed="left" width="50" align="center">
          <template slot-scope="scope">
            <span>{{ (listParams.page - 1) * listParams.page_size + scope.$index + 1 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="工单" prop="work_order_name" fixed="left" width="200" show-overflow-tooltip align="center"
        ></el-table-column>
        <el-table-column label="安装类型" fixed="left" width="100" align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.install_type === 1" size="mini" type="success">新安装</el-tag>
            <el-tag v-if="scope.row.install_type === 2" size="mini">上刊</el-tag>
            <el-tag v-if="scope.row.install_type === 3" size="mini" type="danger">下刊</el-tag>
            <el-tag v-if="scope.row.install_type === 4" size="mini" type="warning">巡检</el-tag>
            <el-tag v-if="scope.row.install_type === 5" size="mini" type="info">维修</el-tag>
            <el-tag v-if="scope.row.install_type === 7" size="mini">换画</el-tag>
            <el-tag v-if="scope.row.install_type === 8" size="mini" type="success">中间监播</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="安装工" fixed="left" prop="worker_name" width="100" align="center"></el-table-column>
				<el-table-column label="保存时间" prop="saved_at" width="150" align="center"></el-table-column>
        <el-table-column label="提交时间" prop="created_at" width="150" align="center"></el-table-column>
        <el-table-column label="楼盘" prop="building_area_name" width="200" show-overflow-tooltip align="center"
        ></el-table-column>
        <el-table-column label="媒体编号" prop="code" width="100" align="center"></el-table-column>
        <el-table-column label="媒体类型" prop="media_type_name" width="150" align="center"></el-table-column>
        <el-table-column label="AB面" width="100" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.ab_type_id == 57">A面</span>
            <span v-if="scope.row.ab_type_id == 58">B面</span>
          </template>
        </el-table-column>
        <el-table-column label="尺寸" width="150" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.size_width&&scope.row.size_height">
              {{ scope.row.size_width + 'CM*' + scope.row.size_height + 'CM' }}
            </span>
          </template>
        </el-table-column>
        <el-table-column label="位置" prop="address" min-width="400" show-overflow-tooltip align="center"
        ></el-table-column>
        <el-table-column label="故障提交" fixed="right" width="100" show-overflow-tooltip align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.breakdown_type ===0" type="info" size="mini">设备完好</el-tag>
            <el-tag v-if="scope.row.breakdown_type ===1" type="danger" size="mini">存在故障</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="状态" fixed="right" width="80" show-overflow-tooltip align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.flow_status === 2" type="danger" size="mini">未通过</el-tag>
            <el-tag v-if="scope.row.flow_status === 3" size="mini">待审核</el-tag>
            <el-tag v-if="scope.row.flow_status === 4" type="success" size="mini">通过</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" width="100" align="center">
          <template slot-scope="scope">
            <el-button icon="el-icon-zoom-in" size="mini" @click="look(scope.row)">查看</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page.sync="searchForm.page"
      :page-sizes="[10,20,50,100, 200, 400]"
      :page-size.sync="searchForm.page_size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <el-dialog title="查看" center width="50%" top="100px" :visible.sync="deviceDetailsVisible" @close="deviceDetail = {}"
    >
      <el-form label-width="120px" label-position="right">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item v-if="deviceDetail != null" label="图片预览">
              <el-carousel :autoplay="false" type="card" height="200px">
                <el-carousel-item v-for="item in deviceDetail.photoList" :key="item">
                  <el-image :src="item" style="height: 200px;width: auto"></el-image>
                </el-carousel-item>
              </el-carousel>
              <el-image style="height:80px;width: 80px;margin: 10px;padding: 5px;border: 1px solid black"
                        v-for="(item,index) in deviceDetail.photoList" :key="index" :src="item"
                        :preview-src-list="deviceDetail.photoList"
              ></el-image>
            </el-form-item>
            <el-form-item label="安装工">
              {{ deviceDetail.worker_name }}
            </el-form-item>
            <el-form-item label="安装类型">
              <span v-if="deviceDetail.install_type == 1">新安装</span>
              <span v-if="deviceDetail.install_type == 2">上刊</span>
              <span v-if="deviceDetail.install_type == 3">下刊</span>
              <span v-if="deviceDetail.install_type == 4">巡检</span>
              <span v-if="deviceDetail.install_type == 5">维修</span>
            </el-form-item>
            <el-form-item label="提交时间">
              {{ deviceDetail.created_at }}
            </el-form-item>
            <el-form-item label="备注">
              {{ deviceDetail.remark }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工单">
              {{ deviceDetail.work_order_name }}
            </el-form-item>
            <el-form-item label="楼盘">
              {{ deviceDetail.building_area_name }}
            </el-form-item>
            <el-form-item label="媒体类型">
              {{ deviceDetail.media_type_name }}
            </el-form-item>
            <el-form-item v-show="deviceDetail.media_type_id==1" label="楼号">
              {{ deviceDetail.building_number + '号楼' }}
            </el-form-item>
            <el-form-item v-show="deviceDetail.media_type_id==1" label="单元号">
              {{ deviceDetail.unit_number + '单元' }}
            </el-form-item>
            <el-form-item v-show="deviceDetail.media_type_id==1" label="楼层">
              {{ deviceDetail.floor + '层' }}
            </el-form-item>
            <el-form-item v-show="deviceDetail.media_type_id==1" label="楼栋类型">
              {{ deviceDetail.building_type == 1 ? '多层' : '高层' }}
            </el-form-item>
            <el-form-item v-show="deviceDetail.media_type_id==9" label="门号">
              {{ deviceDetail.door_number + '号门' }}
            </el-form-item>
            <el-form-item v-show="deviceDetail.media_type_id==9" label="AB面">
              {{ deviceDetail.ab_type_id == 57 ? 'A面' : 'B面' }}
            </el-form-item>
            <el-form-item v-show="deviceDetail.media_type_id==9" label="是否亮灯">
              {{ deviceDetail.is_light == 0 ? '不亮' : '亮灯' }}
            </el-form-item>
            <el-form-item label="媒体编号">
              {{ deviceDetail.code }}
            </el-form-item>
            <el-form-item v-if="deviceDetail.breakdown_type === 1" label="故障原因" style="text-align: left">
              <el-tag style="margin-right: 5px" v-for="(item,index) in deviceDetail.breakdown_type_name" :key="index"
                      size="mini"
                      type="danger"
              >{{ item }}
              </el-tag>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="deviceDetailsVisible = false">关 闭</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import SearchFormBox from '@/components/SearchFormBox'
import { list_all_simple, list_media_types } from '@/api/building/building'
import { exportExcel, page } from '@/api/workOrder/workSubmit'
import { allList, listWorker } from '@/api/workOrder/workOrder'

export default {
  name: 'index.vue',
  components: {
    SearchFormBox
  },
  data() {
    return {
      searchForm: {
        page: 1,
        page_size: 10,
        begin_time: null,
        end_time: null,
				begin_time_save: null,
				end_time_save: null,
        work_order_id: null,
        worker_id: null,
        work_order_type_ids: [],
        status: null,
        code: null,
        building_area_id: null,
        media_type_id: null,
        statistics: false
      },
      listParams: {
        page: 1,
        page_size: 10,
        begin_time: null,
        end_time: null,
				begin_time_save: null,
				end_time_save: null,
        work_order_id: null,
        worker_id: null,
        work_order_type_ids: [],
        status: null,
        code: null,
        building_area_id: null,
        media_type_id: null,
        statistics: false
      },
      total: 0,
      buildingArr: [],
      allMediaTypeArr: [],
      tableData: [],
      tableLoading: false,
      deviceDetail: {},
      workerArr: [],
      workorderArr: [],
      deviceDetailsVisible: false
    }
  },
  created() {
    this.initALlMediaTypeArr()
    this.initBuildingArr()
    this.initWorkerArr()
    this.initWorkOrderArr()
    this.getList()
  },
  methods: {
    initALlMediaTypeArr() {
      list_media_types().then(res => {
        this.allMediaTypeArr = res.data
      })
    },
    initBuildingArr() {
      list_all_simple().then(res => {
        this.buildingArr = res.data
      })
    },
    initWorkerArr() {
      listWorker().then(res => {
        this.workerArr = res.data
      })
    },
    search() {
      this.searchForm.page = 1
      this.listParams = Object.assign({}, this.searchForm)
      this.getList()
    },
    getList() {
      this.tableLoading = true
      page(this.listParams).then(res => {
        this.tableLoading = false
        this.tableData = res.data.records
        this.total = res.data.total
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
      this.listParams = Object.assign({}, this.searchForm)
      this.getList()
    },
    handleSizeChange(val) {
      this.listParams.page_size = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listParams.page = val
      this.getList()
    },
    look(row) {
      this.deviceDetailsVisible = true
      this.deviceDetail = row
    },
    tableRowClassName({ row, rowIndex }) {
      if (row.deleted) {
        return 'danger-row'
      } else {
        return ''
      }
    },
    exportExcel() {
      exportExcel(this.searchForm)
    },
    initWorkOrderArr() {
      allList().then(res => {
        this.workorderArr = res.data
      })
    },
    statisticsChange(val) {
      if (val) {
        this.searchForm.status = null
      }
    }
  }
}
</script>

<style scoped lang="scss">
.page_main {
  margin-top: 20px;
  padding: 0 20px;
}

.data_box {
  margin-top: 10px;
}

/deep/ .el-table .danger-row {
  color: #e54d42;
}
</style>
